﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using BitPortal.Business;
using BitPortal.Utils;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
namespace BitPortal.Controls
{
    public class BaseFrontendPage : System.Web.UI.Page
    {
        /// <summary>
        /// Set Language
        /// </summary>
        protected override void InitializeCulture()
        {
            string culture = Business.BitContext.Current.Lang.LanguageCode;
            if (string.IsNullOrEmpty(culture)) culture = "vi-VN";
            //Use this
            this.UICulture = culture;
            this.Culture = culture;
            //OR This
            if (culture != "vi-VN")
            {
                System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(culture);
                System.Threading.Thread.CurrentThread.CurrentCulture = ci;
                System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
            }
            base.InitializeCulture();

        }

        /// <summary>
        /// Set Theme
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_PreInit(object sender, EventArgs e)
        {
            this.Theme = BitContext.Current.Theme;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            BindMetaData();
        }

        public virtual void IncludeCssFile(string path)
        {
            HtmlLink css = new HtmlLink();
            css.Href = path;
            css.Attributes["rel"] = "stylesheet";
            css.Attributes["type"] = "text/css";
            css.Attributes["media"] = "all";
            css.ID = path.GetHashCode().ToString();
            if (Header.FindControl(path.GetHashCode().ToString()) == null) this.Header.Controls.Add(css);
        }

        public virtual void IncludeScriptFile(string path)
        {
            Literal ltr = new Literal() { ID = path.GetHashCode().ToString(), Text = "<script type=\"text/javascript\" src=\"" + path + "\"></script>" };
            if (this.Header.FindControl(path.GetHashCode().ToString()) == null) this.Header.Controls.Add(ltr);
        }

        /// <summary>
        /// Alert a message
        /// </summary>
        /// <param name="message"></param>
        /// <param name="title"></param>
        /// <param name="callback"></param>
        public virtual void ShowArlert(string message, string title = "Thông báo", string callback = "function(){}")
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "BoxyAlert", "Boxy.alert(\"" + message + "\"," + callback + ", {title:\"" + title + "\"});", true);
        }

        /// <summary>
        /// ShowConfirm
        /// </summary>
        /// <param name="message"></param>
        /// <param name="title"></param>
        /// <param name="callback"></param>
        public virtual void ShowConfirm(string message, string title = "Thông báo", string callback = "function(){}")
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "BoxyAlert", "Boxy.confirm(\"" + message + "\"," + callback + ", {title:\"" + title + "\"});", true);
        }

        /// <summary>
        /// Boxy a div by id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="title"></param>
        /// <param name="modal"></param>
        public virtual void ShowPanel(string id, string title = "Thông báo", bool modal = true)
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "BoxyAlert", "BoxyByID('" + id + "','" + title + "', " + modal + ");", true);
        }

        /// <summary>
        /// Register Client Script
        /// </summary>
        /// <param name="script"></param>
        /// <param name="key"></param>
        public virtual void RegisterScript(string script, string key = null)
        {
            if (key == null) key = "Key" + DateTime.Now.GetHashCode();
            ScriptManager.RegisterStartupScript(this, typeof(Page), key, script, true);
        }
        /// <summary>
        /// Bind dữ liệu meta như: các thẻ meta phục vụ SEO, thanh navigation
        /// </summary>
        public virtual void BindMetaData()
        {

        }

        /// <summary>
        /// Check login tu client side
        /// </summary>
        /// <returns></returns>
        [System.Web.Services.WebMethod]
        public virtual bool CheckLogin()
        {
            bool flag = (Membership.GetUser() != null);
            if (!flag)
            {
                RegisterScript("Boxy.get(jQuery('#QuickLoginPanel')).show();");
            }
            return flag;
        }
    }

}